package com.wz.datachannel.util;


import com.wz.datachannel.R;

import android.content.Context;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

/**
 * Toast工具类
 * 
 * @author LW
 *
 */
public class ToastUtil extends Toast {
	private TextView textView;

	private ToastUtil(Context context) {
		super(context);
		init(context);
	}

	private void init(Context context) {
		View view = View.inflate(context, R.layout.toast_view, null);
		textView = (TextView) view.findViewById(R.id.toast);
		setView(view);
	}

	private final static Boolean isToast = true;
	private static ToastUtil toast = null;

	/**
	 * Toast发送消息，默认Toast.LENGTH_LONG
	 * 
	 * @param context
	 * @param text
	 */
	public static void toastShowLong(final Context context, final String text) {
		if (isToast) {
			showToast(context, text, Toast.LENGTH_LONG);
		}
	}

	/**
	 * Toast发送消息，默认Toast.LENGTH_SHORT
	 * 
	 * @param context
	 * @param text
	 */
	public static void toastShowShort(final Context context, final String text) {
		if (isToast) {
			showToast(context, text, Toast.LENGTH_SHORT);
		}
	}

	@Override
	public void setText(CharSequence s) {
		textView.setText(s);
	}

	@Override
	public void setText(int resId) {
		textView.setText(resId);
	}

	private static void showToast(Context context, String text, int duration) {
		if (toast == null) {
			toast = new ToastUtil(context);
		}
		toast.setDuration(duration);
		toast.setText(text);
		toast.show();
	}
}
